package daoMySQL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import pnlRestaurant.PnlResMain;
import daoDataFactory.DbObject;
import daoInterface.DaoITafel;
import domijn.Tafel;

public class MySQLTafel  extends DbObject implements DaoITafel {

	private Statement stmt = null;
	private ResultSet rs = null;
	public PnlResMain pnlResMain ;
	
	private static MySQLTafel instance;
	
	public static DaoITafel instance(){
		if (instance == null) {
			 instance = new MySQLTafel();
		}
		return instance;
	}

	public boolean save(Tafel t) {
		 String sqlSave = "INSERT INTO `tafel`(TafelNummer)"
					+ "	VALUES("
					+ "		'"+ t.getTflNumber()+"'"
				    + ")";
		try{
			 stmt = openConnection().createStatement();
			 stmt.executeUpdate(sqlSave);
			 
//			 pnlResMain.getLblSaveMelding().setVisible(true);
//			 pnlResMain.getLblDeleteMelding().setVisible(false);
//			 pnlResMain.revalidate();
//			 pnlResMain.repaint();
			 
			 System.out.println("MySQLTafel, public void save, INSERT INTO");
			 return true;
			}
		catch(SQLException sql1){
			 if (sql1.getErrorCode()==1062) {
				 JOptionPane.showMessageDialog(null, "<html><b> "
												  + "Deze tafel bestaat al. <br>"
												  + "Voer a.u.b. een ander tafel nummer in. "
												  + "</b></html>");
			 									  }
			else{
				 System.out.println("Table is saved");
				   
					sql1.getErrorCode();
			}}
		
	return false;
	}

	
	public void update(Tafel t) {
		String sqlUpdate = "UPDATE"
				+ "		`table`"
				+ "	SET"
				+ "		`table`.`number` ='"+ t.getTflNumber()+"'"
				+ "	WHERE"
				+ "		`table`.`number` ='"+ t.getTflNumber()+"'"; 
		try{
			stmt = openConnection().createStatement();
			stmt.executeUpdate(sqlUpdate);
					}
		catch(Exception e1){
			
		}}

	
	public boolean delete(Tafel t) {
		String sqlDelete = "DELETE FROM"
				+"		`tafel`"
				+"	WHERE"
				+"		`tafel`.`TafelNummer` = '" + t.getTflNumber()+ "'";
		try {
			stmt = openConnection().createStatement();
			stmt.executeUpdate(sqlDelete);
					} 
		catch (Exception e2) {
			e2.printStackTrace();
		}
		return false;
		}


	
	public Tafel find(Tafel t) {
		String sqlFind =  "Select * from table where number = " + t.getTflNumber();
		try {
			stmt = openConnection().createStatement();
			rs = stmt.executeQuery(sqlFind);
			rs.next();
		} catch (SQLException sql2) {
			System.out.println(sql2.getErrorCode());
			System.out.println("Tafel nummer niet gevonden");
		}
		return t;
	}

	
	public ArrayList<Tafel> loadall() {
		ArrayList<Tafel> tables = new ArrayList<Tafel>();
		Tafel t;
		String sqlLoadall = "Select * from tafel ORDER BY `tafel`.`TafelNummer`";
		try {
			stmt = openConnection().createStatement();
			rs = stmt.executeQuery(sqlLoadall);
			while (rs.next()) {
				t = new Tafel(rs.getInt(2));
				tables.add(t);
				}} 
			catch (Exception e) {
			}
		return tables;
	} 

}
